// pages/home/home.js
const app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    showPage:false,
    showAuth: false,
    examList: [],
    paperList: [],
    authObj: {
      title: '登录提示',
      content: '您只有授权了，才可以参加考试哦！',
      confirmText: '知道了'
    },
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    wx.showLoading()
    this.login()
  },
  getMockPaper(){
    app.api.getMockPaper()
    .then(res => {
      let arr = res.content
      let examList = []
      let paperList = []
      arr.forEach(item => {
        if (item.simulation == 0){
          examList.push(item)
        } else if (item.simulation == 1){
          paperList.push(item)
        }
      })
      this.setData({
        examList: examList,
        paperList: paperList.reverse()
      })
    })
  },
  //用户登陆获取登陆态
  login() {
    wx.login({
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
        var _this = this
        // return
        if (res.code) {
          wx: wx.request({
            url: 'https://t.weijiayingxiao.com/login',
            method: "POST",
            header: {
              "Content-Type": "application/x-www-form-urlencoded",
              'cookie': wx.getStorageSync('session')
            },
            data: {
              code: res.code
            },
            success: function (res) {
              _this.saveSession(res)
            }
          })
        }
      }
    })
  },
  // 保存用户session到本地
  saveSession: function (res) {
    // console.log(res)
    var _this = this
    var sessionId = (res.header['set-cookie'] || res.header['Set-Cookie']) && (res.header['set-cookie'] || res.header['Set-Cookie']).split(';')[0]
    if (sessionId) {
      wx.setStorage({
        key: 'session',
        data: sessionId,
        success: function () {
          _this.saveInfo()
        }
      })
    } else {
      _this.saveInfo()
    }
  },
  bindGetUserInfo: function (e) {
    this.setData({
      showAuth: false
    })
    this.saveInfo()
  },
  saveInfo: function () {
    var _this = this
    wx.getUserInfo({
      success: function (data) {
        wx.hideLoading()
        _this.setData({
          showPage: true
        })
        var dataObj = {
          encryptedData: data.encryptedData,
          iv: data.iv
        }
        app.api.saveUserInfo(dataObj)
        .then(res=>{
          app.globalData.nickname = res.name || res.nickname
          app.globalData.icon = res.icon
          app.globalData.userId = res.id
          _this.getMockPaper()
        })
      },
      fail: function (data) {
        wx.hideLoading()
        _this.setData({
          showPage: true,
          showAuth: true
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})